package cn.icanci.loopstack.bic.dal.mongo.mongo;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import cn.icanci.loopstack.bic.dal.mongo.common.PageList;
import cn.icanci.loopstack.bic.dal.mongo.daointerface.EdgeDAO;
import cn.icanci.loopstack.bic.dal.mongo.dateobject.EdgeDO;

/**
 * @author icanci
 * @since 1.0 Created in 2023/09/16 10:02
 */
@Service("edgeDAO")
public class MongoEdgeDAO extends AbstractBaseDAO<EdgeDO> implements EdgeDAO {
    @Override
    public void insert(EdgeDO edgeDO) {
        super.insert(edgeDO);
        mongoTemplate.insert(edgeDO, COLLECTION_NAME);
    }

    @Override
    public void update(EdgeDO edgeDO) {
        super.update(edgeDO);
        mongoTemplate.save(edgeDO, COLLECTION_NAME);
    }

    @Override
    public List<EdgeDO> queryAll() {
        Criteria criteria = Criteria.where(EdgeColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.find(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public PageList<EdgeDO> pageQuery(EdgeDO edgeDO, int pageNum, int pageSize) {
        Criteria criteria = Criteria.where(EdgeColumn.ENV).is(DEFAULT_ENV);
        if (StringUtils.isNotBlank(edgeDO.getEdgeName())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(EdgeColumn.EDGE_NAME).regex("^.*" + edgeDO.getEdgeName() + ".*$", "i");
        }
        if (StringUtils.isNotBlank(edgeDO.getEdgeKey())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(EdgeColumn.EDGE_KEY).regex("^.*" + edgeDO.getEdgeKey() + ".*$", "i");
        }
        if (StringUtils.isNotBlank(edgeDO.getSystemUuid())) {
            criteria.and(EdgeColumn.SYSTEM_UUID).is(edgeDO.getSystemUuid());
        }
        Query query = new Query(criteria);
        query.with(Sort.by(Sort.Direction.DESC, EdgeColumn.CREATE_TIME));
        return pageQuery(query, COLLECTION_CLASS, pageSize, pageNum, COLLECTION_NAME);
    }

    @Override
    public EdgeDO queryOneById(String _id) {
        Criteria criteria = Criteria.where(EdgeColumn._ID).is(_id);
        criteria.and(EdgeColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public Class<? extends BaseColumn> getBaseColumn() {
        return EdgeColumn.class;
    }

    @Override
    public EdgeDO queryBySystemUuidAndName(String systemUuid, String name) {
        Criteria criteria = Criteria.where(EdgeColumn.EDGE_NAME).is(name);
        criteria.and(EdgeColumn.ENV).is(DEFAULT_ENV);
        criteria.and(EdgeColumn.SYSTEM_UUID).is(systemUuid);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public EdgeDO queryBySystemUuidAndKey(String systemUuid, String key) {
        Criteria criteria = Criteria.where(EdgeColumn.EDGE_KEY).is(key);
        criteria.and(EdgeColumn.ENV).is(DEFAULT_ENV);
        criteria.and(EdgeColumn.SYSTEM_UUID).is(systemUuid);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }
}
